//  Copyright (C) 2008-2009  The Aceituna Project Team
//  Portions of this code are Copyright(C) 2002-2007  The FreeCol Team (www.freecol.org).

package com.aceitunaproject.webclient.client;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.ImageElement;

import gwt.gwtisometricboard.client.ImageProvider;
import gwt.gwtisometricboard.client.model.Thing;
import gwt.gwtisometricboard.client.model.Tile;

public class BoardImageProvider implements ImageProvider {


	
	public BoardImageProvider() {
	
	}

	@Override
	public ImageElement getCenter(Tile tile) {
		ImageElement image = Document.get().createImageElement();
		String breakPattern = "0";
		// Pattern breaking code Copyright(C) 2002-2007 The FreeCol Team
		// (www.freecol.org).
		if (tile.getX() / (tile.getY() + 1) % 2 == 0) {
			// the pattern is mostly visible on ocean tiles
			// this is an attempt to break it up so it doesn't create big
			// stripes or chess-board effect
			breakPattern = "1";
		} else {
			breakPattern = "0";
		}

		image.setSrc(Document.get().getURL().substring(0,
				Document.get().getURL().indexOf("/"))
				+ "images/tiles/"
				+ tile.getBasicTileType().toLowerCase()
				+ "/center"
				+ breakPattern + ".png");


		return image;
	}

	@Override
	public ImageElement getThingImage(Thing thing) {

		ImageElement image = Document.get().createImageElement();
		image.setSrc(Document.get().getURL().substring(0,
				Document.get().getURL().indexOf("/"))
				+ "images/unit.png");

		return image;
	}

	@Override
	public ImageElement getBorder(Tile tile, Tile tile2) {

		if (tile2 == null)
			return null;
		if (tile.getBasicTileType() == tile2.getBasicTileType()
				|| tileHasPrecedence(tile, tile2)) {
			return null;
		} else {
			ImageElement image = Document.get().createImageElement();

			String direction = determineDirection(tile, tile2);

			String breakPattern = "even";
			// Pattern breaking code Copyright(C) 2002-2007 The FreeCol Team
			// (www.freecol.org).
			if ((tile.getX() + tile.getY()) % 2 != 0) {

				breakPattern = "odd";
			}

			image.setSrc(Document.get().getURL().substring(0,
					Document.get().getURL().indexOf("/"))
					+ "images/tiles/"
					+ tile2.getBasicTileType().toLowerCase()
					+ "/border_"
					+ direction + "_" + breakPattern + ".png");

			return image;
		}
	}

	private boolean tileHasPrecedence(Tile tile, Tile tile2) {
		// uses the basic tile type length for now, ideally there should be a
		// precedence table or something.
		return tile.getBasicTileType().length() <= tile2.getBasicTileType()
				.length();
	}

	private String determineDirection(Tile tile, Tile tile2) {

		if (tile.getX() + 1 == tile2.getX() && tile.getY() + 1 == tile2.getY()) {
			return "E";
		}
		if (tile.getX() - 1 == tile2.getX() && tile.getY() - 1 == tile2.getY()) {
			return "W";
		}
		if (tile.getX() == tile2.getX() && tile.getY() + 1 == tile2.getY()) {
			return "SE";
		}
		if (tile.getX() == tile2.getX() && tile.getY() - 1 == tile2.getY()) {
			return "NW";
		}

		if (tile.getX() - 1 == tile2.getX() && tile.getY() == tile2.getY()) {
			return "SW";
		}

		if (tile.getX() + 1 == tile2.getX() && tile.getY() == tile2.getY()) {
			return "NE";
		}

		if (tile.getX() + 1 == tile2.getX() && tile.getY() - 1 == tile2.getY()) {
			return "N";
		}

		if (tile.getX() - 1 == tile2.getX() && tile.getY() + 1 == tile2.getY()) {
			return "S";
		}
		return "";

	}

	
}
